package com.mingda.weathermagiccenter.utils.lzugis.geotools.utils;

import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

public class GeoUtils {
    /**
     * 通用几何对象工厂构建器
     */
    private static final GeometryFactory FACTORY = JTSFactoryFinder.getGeometryFactory(null);
    private static final WKTReader WKT_READER = new WKTReader(FACTORY);

    /**
     * 判断点是否存在多边形内
     *
     * @param point   点
     * @param polygon 多边形
     * @return true - 在多边形内
     * @throws ParseException wkt转换异常
     * @see <a href="https://blog.csdn.net/lidejun152046/article/details/47128169">WKT格式</a>
     */
    public static boolean contains(String point, String polygon) throws ParseException {
        return WKT_READER.read(polygon).contains(WKT_READER.read(point));
    }

    /**
     * geojson 转换成 geometry
     *
     * @param geojson
     * @return
     * @throws IOException
     */
    public static Geometry geojson2Geometry(String geojson) throws IOException {
        GeometryJSON gjson = new GeometryJSON(7);
        return gjson.read(new StringReader(geojson));
    }

    public static Geometry wtk2point(String wtk) throws ParseException {
        return WKT_READER.read(wtk);
    }

    /**
     * geometry 转换成 geojson
     *
     * @param geometry
     * @return
     * @throws IOException
     */
    public static String geometry2Geojson(Geometry geometry) throws IOException {
        GeometryJSON gjson = new GeometryJSON(7);
        StringWriter writer = new StringWriter();
        gjson.write(geometry, writer);
        return writer.toString();
    }

    /**
     * @param wkt
     * @return
     * @throws ParseException
     */
    public Geometry wkt2Geometry(String wkt) throws ParseException {
        WKTReader reader = new WKTReader();
        return reader.read(wkt);
    }

    /**
     * @param geometry
     * @return
     * @throws ParseException
     */
    public String geometry2Wkt(Geometry geometry) throws ParseException {
        WKTWriter writer = new WKTWriter();
        return writer.write(geometry);
    }


    public static void main(String[] args) {
        try {
            String wktPoint1 = "POINT (10 10)";
            String wktPoint2 = "POINT (20 20)";
            String wktPoly = "POLYGON ((0 0, 0 10, 10 20, 20 20, 20 0, 0 0))";
            boolean test1 = GeoUtils.contains(wktPoint1, wktPoly);
            boolean test2 = false;
            test2 = GeoUtils.contains(wktPoint2, wktPoly);
            System.out.println("point1位置：" + test1); // true
            System.out.println("point2位置：" + test2); // false
            String geojson = "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[124.81447968,45.1696419,0],[124.82374788,45.16992378,0],[124.82484264,45.16983882,0],[124.82495676,45.16982811,0],[124.82530164,45.16978869,0],[124.82578152,45.16981362,0],[124.83010332,45.17036757,0],[124.83448488,45.17086959,0],[124.8348816,45.17086041,0],[124.83510012,45.17082081,0],[124.83530352,45.17075916,0],[124.841106,45.1674594,0],[124.84054368,45.1538811,0],[124.82456796,45.15615504,0],[124.81240392,45.15844239,0],[124.80656076,45.15954102,0],[124.81011252,45.16943121,0],[124.81447968,45.1696419,0]]],[[[124.81447968,45.1696419,0],[124.81445556,45.16975683,0],[124.81445772,45.17004474,0],[124.81447968,45.1696419,0]]]]}";
            Geometry point = WKT_READER.read("POINT Z (128.0559582 42.00874173 0)");
            Geometry point1 = WKT_READER.read(wktPoint2);
            Geometry area = GeoUtils.geojson2Geometry(geojson);
            System.out.println(area.contains(point));
            System.out.println("point>>>>" + area.getCentroid().getY() + ":::::" + area.getCentroid().getX());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

